//
//  XMVisitorView.swift
//  XMWeiCreatProject
//
//  Created by XM on 16/8/8.
//  Copyright © 2016年 XM. All rights reserved.
//

import UIKit
import SnapKit

class XMVisitorView: UIView {
    
    
    //是否是首页 决定有没有转盘
    func baseControlInit(isHome:Bool, charsStr:String, mainIcon:String) {
        if !isHome {
            zhuanPanImage.hidden = true
            hudImage.hidden = true
            profileLabel.text = charsStr
            fangziImage.image = UIImage(named: mainIcon)
        }
        
        zhuanpanStartAnimation()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(zhuanPanImage)
        addSubview(hudImage)
        addSubview(fangziImage)
        addSubview(profileLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //布局子控件之间的约束
        setupChildsViewContraints()
    }
    
    //Swift中要么init 要么xib/storyboard 来创建UIView  此处把xib/storyboard给扼杀了
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func zhuanpanStartAnimation() {
        let zhuan = CABasicAnimation(keyPath: "transform.rotation")
        zhuan.duration = 5
        zhuan.repeatCount = MAXFLOAT
        zhuan.byValue = 2 * M_PI
        //逆序
        zhuan.autoreverses = true
        //解决控制器跳转后 再次返回会销毁转盘动画
        zhuan.removedOnCompletion = false
        
        zhuanPanImage.layer.addAnimation(zhuan, forKey: nil)
    }
    
    
    private func setupChildsViewContraints() {
        
        zhuanPanImage.snp_makeConstraints { (make) in
            make.center.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
        }
        
        hudImage.snp_makeConstraints { (make) in
            make.center.equalTo(zhuanPanImage)
        }
        
        fangziImage.snp_makeConstraints { (make) in
            make.center.equalTo(zhuanPanImage)
        }
        
        profileLabel.snp_makeConstraints { (make) in
            make.top.equalTo(hudImage.snp_bottom).offset(10)
            make.left.equalTo(self).offset(40)
            make.right.equalTo(self).offset(-40)
        }
        
        registerBtn.snp_makeConstraints { (make) in
            make.left.equalTo(profileLabel.snp_left)
            make.top.equalTo(profileLabel.snp_bottom).offset(20)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
        loginBtn.snp_makeConstraints { (make) in
            make.right.equalTo(profileLabel.snp_right)
            make.size.top.equalTo(registerBtn)
        }
        
    }
    
    
//    //MARK closures 闭包已经测试 这个也放弃
//    let callBack:() -> Void = { () -> Void in
//        
//    }
//    //
//    func loginBtnClick() {
//        callBack()
//    }
    
    
    // MARK: -> lazyloadchilds
    private var zhuanPanImage: UIImageView = {
        let img = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return img
    }()
    
    private var fangziImage: UIImageView = {
        let img = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return img
    }()
    
    private var hudImage: UIImageView = {
        let img = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return img
    }()
    
    private var profileLabel: UILabel = {
        let profile = UILabel()
        profile.textColor = UIColor.lightGrayColor()
        profile.text = "啊是浪费和破坏大家哦间日军噶叫时过类发票完全工具破解更改为骗取均"
        profile.font = UIFont.systemFontOfSize(12.0)
        profile.numberOfLines = 0
        return profile
    }()
    
    private var loginBtn: UIButton = {
        let login = UIButton()
        login.setTitle("登  录", forState: UIControlState.Normal)
        login.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        login.titleLabel?.font = UIFont.systemFontOfSize(13.0)
        login.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //MARK -> BUG 在View视图中添加button 添加addTarget就会报错
        //login.addTarget(self as? AnyObject, action:#selector(XMVisitorView.loginBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        return login
    }()
    
    private var registerBtn: UIButton = {
        let register = UIButton()
        register.setTitle("注  册", forState: UIControlState.Normal)
        register.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        register.titleLabel?.font = UIFont.systemFontOfSize(13.0)
        register.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return register
    }()
}
